package edu.upf.opencities.web.dao.impl;

import java.util.Random;

import org.apache.log4j.Logger;

import edu.upf.opencities.datamodel.*;
import edu.upf.opencities.web.dao.ServiceDAO;
import edu.upf.opencities.web.dao.UserDAO;

public class DAOImplMock implements ServiceDAO, UserDAO {

	
	private static DAOImplMock dao = null;
	public static Logger logger = Logger.getLogger(DAOImpl.class);
	
	
	//Mock objects
	User user = new User();
	Services services = new Services();
	boolean first =true;
	
	void initMock(){
		Service service = new Service();
		service.setAuthor("victor");
		//service.setDate(DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar()));
		service.setDescription("temperature");
		service.setEmail("vic.valls@gmail.com");
		service.setExtras(new Extras());
		service.setId("1234");
		service.setLatitude("41.40265");
		service.setLongitude("2.1957");
		if(first){
			service.setName("TemperatureUPF");
		}
		else{
			service.setName("TemperatureUPF"+new Random().nextInt());
		}
		service.setPassword("gato");
		service.setGroup("environmental");
		service.setUpdatePeriod("5");
		service.setUrl("nets.upf.edu");
		
		services.getServices().add(service);
		
		user.setCompany("UPF");
		user.setCountry("Spain");
		user.setEmail("vic.valls@gmail.com");
		user.setLastName("Valls");
		user.setName("victor");
		user.setPassword("gato");
	}
	
	//Singleton
	public static synchronized DAOImplMock getInstace() {
		if (null == dao) {
			dao = new DAOImplMock();
		}
		return dao;
	}
	
	@Override
	public Service registerService(Service service, User user) {
		services.getServices().add(service);
		return service;	
	}

	@Override
	public boolean deleteService(Service service, User user) {
		services.getServices().remove(0);
		return true;	
	}

	@Override
	public Service getOneService(String idService) {
		initMock();
		return services.getServices().get(0);	
	}

	@Override
	public Services getServicesByProperty(String property, String content) {
		initMock();
		return services;
	}

	@Override
	public String addEntry(Service service, Entry entry) {
		
		return "success";
	}

	@Override
	public Service getLastEntry(String idService) {
		return services.getServices().get(0);
	}

	@Override
	public void addTwitter(Service service, User user) {
	
	}

	@Override
	public Services getEntriesFromService(String idService, int numEntries) {
		return services;
	}

	//USER
	@Override
	public boolean registerUser(User user) {
		return true;
	}

	@Override
	public boolean userUpdate(User user) {
		this.user = user;
		return true;
	}

	@Override
	public User getUser(User user) {
		initMock();
		return this.user;
	}

	@Override
	public void deleteUser(User user) {
		
	}

	@Override
	public boolean loginUser(User user) {
		return true;
	}
	

}
